﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="kapitaliJagunemineType", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(KapitaliJagunemineType))]
    public class KapitaliJagunemineTypeSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IKapitaliJagunemineType clazz;
        
        public KapitaliJagunemineTypeSerializer(IKapitaliJagunemineType clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("id");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "integer");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInteger(((decimal)(clazz.Id))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("tegevus");
            if (clazz.Tegevus!=null) {
                Writer.WriteTypeAttribute("http://producers.arireg.xtee.riik.ee/producer/arireg", "x_tegevus");
                Writer.WriteString(clazz.Tegevus.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Tegevus");
            }
            Writer.WriteEndElement();
            if (clazz.Omand!=null) {
                Writer.WriteStartElement("omand");
                global::Xtee.Core.SerializationMetadata metaOmand = this.GetMetaData(clazz.Omand);
                Writer.WriteTypeAttribute(metaOmand.TypeQualifiedName.Namespace, metaOmand.TypeQualifiedName.Name);
                metaOmand.NewSerializer(clazz.Omand).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.Osapandid!=null) {
                Writer.WriteStartElement("osapandid");
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("osapantType", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Osapandid.Count);
                if (((clazz.Osapandid == null) 
                            || (clazz.Osapandid.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Osapandid != null) 
                            && (x < clazz.Osapandid.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Osapandid[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaOsapandid = this.GetMetaData(clazz.Osapandid[x]);
                        Writer.WriteTypeAttribute(metaOsapandid.TypeQualifiedName.Namespace, metaOsapandid.TypeQualifiedName.Name);
                        metaOsapandid.NewSerializer(clazz.Osapandid[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
                Writer.WriteEndElement();
            }
            if (clazz.TingimuslikYleminek!=null) {
                Writer.WriteStartElement("tingimuslik_yleminek");
                global::Xtee.Core.SerializationMetadata metaTingimuslikYleminek = this.GetMetaData(clazz.TingimuslikYleminek);
                Writer.WriteTypeAttribute(metaTingimuslikYleminek.TypeQualifiedName.Namespace, metaTingimuslikYleminek.TypeQualifiedName.Name);
                metaTingimuslikYleminek.NewSerializer(clazz.TingimuslikYleminek).WriteXml(Writer);
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType>id");
                }
                clazz.Id = new global::Xtee.Core.Schema.XsdInteger(Reader.ReadElementString("id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>kapitaliJagunemineType>id", Reader.LocalName);
            }
            if (Reader.LocalName=="tegevus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType>tegevus");
                }
                clazz.Tegevus = new global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.XTegevus(Reader.ReadElementString("tegevus"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>kapitaliJagunemineType>tegevus", Reader.LocalName);
            }
            if (Reader.LocalName=="omand") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType>omand");
                }
                global::Xtee.Core.SerializationMetadata metaOmand = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOmandType>(Reader, "omandType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Omand = metaOmand.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOmandType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("omand");
                    metaOmand.NewSerializer(clazz.Omand).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="osapandid") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType>osapandid");
                }
                clazz.Osapandid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("osapandid");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaOsapandid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantType>(Reader, "osapantType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                                global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantType item = metaOsapandid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IOsapantType>();
                                clazz.Osapandid.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    Reader.ReadStartElement("item");
                                    metaOsapandid.NewSerializer(item).ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Osapandid.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">item", Reader.LocalName);
                        }
                    }
                    if ((clazz.Osapandid.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="tingimuslik_yleminek") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>kapitaliJagunemineType>tingimuslik_yleminek");
                }
                global::Xtee.Core.SerializationMetadata metaTingimuslikYleminek = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKitsendusedType>(Reader, "kitsendusedType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.TingimuslikYleminek = metaTingimuslikYleminek.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKitsendusedType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("tingimuslik_yleminek");
                    metaTingimuslikYleminek.NewSerializer(clazz.TingimuslikYleminek).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
        }
    }
}
